const app = getApp()
Page({
  data: {
    type: '',
    announce: {},
    remind: {}
  },

  // 获取公告内容
  getAnnounce(announce) {
    app.sendPostRequest('/community/message/getAnnounce', announce, (res) => {
      console.log(res)
      if(res.code == 200) {
        announce.id = res.data.id,
        announce.type = res.data.type,
        announce.content = res.data.content,
        announce.createTime = res.data.createTime
        this.setData({
          announce
        })
      }
    }, (err) => {
      console.log(err.errMsg)
    })
  },
  // 获取提醒内容
  getRemind(remind) {
    app.sendPostRequest('/community/message/getRemind', remind, (res) => {
      console.log(res)
      if(res.code == 200) {
        remind.id = res.data.id,
        remind.type = res.data.senderAction,
        remind.object = res.data.object,
        remind.objectType = res.data.objectType,
        remind.createTime = res.data.createTime
        this.setData({
          remind
        })
      }
    }, (err) => {
      console.log(err.errMsg)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
    if(options.type == 'announce') {
      let announce = {
        announceId: options.id,
        status: options.status
      }
      this.getAnnounce(announce)
    } else if (options.type == 'remind') {
      let remind = {
        id: options.id,
        status: options.status
      }
      this.getRemind(remind)
    }
  },

  /**
   * 不使用这种方式
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    // var pages = getCurrentPages(); //当前页面
    // var prevPage = pages[pages.length - 2]; //前一页
    // if(this.data.type == 'announce') {
    //   prevPage.setData({
    //     announceList: []
    //   })
    //   prevPage.getUnreadCount('getUnreadAnnounceCount');
    //   prevPage.getAnnounceList(0);
    // } else if(this.data.type == 'remind') {
    //   prevPage.setData({
    //     remindList: []
    //   })
    //   prevPage.getUnreadCount('getUnreadRemindCount')
    //   prevPage.getRemindList(0)
    // }
  }
})